package com.tech.connect.activity;

import android.content.Intent;
import android.os.Bundle;

import com.gyf.barlibrary.ImmersionBar;
import com.ksy.common.activity.CommonBaseActivity;
import com.ksy.common.dialog.ItemsDialog;
import com.tech.connect.R;
import com.tech.connect.huanzhewan.HuanZheWanMainFragment;
import com.tech.connect.zhaofuwu.ZhaoFuWuMainFragment;
import com.tech.connect.zhaorencai.RenCaiMainFragment;

public class ModelMainActivity extends BaseActivity {

    private int currentIndex = -1;
    private RenCaiMainFragment renCaiMainFragment;
    private ZhaoFuWuMainFragment zhaoFuWuMainFragment;
    private HuanZheWanMainFragment huanZheWanMainFragment;
    private ItemsDialog itemsDialog;
    private TitleListener titleListener;

    @Override
    protected void initImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar
                .transparentStatusBar()
                .fitsSystemWindows(false)
                .keyboardEnable(true)
                .init();
    }

    @Override
    protected boolean isShowHeadBar() {
        return false;
    }

    public static void jump(CommonBaseActivity activity, int index) {
        Intent intent = new Intent(activity, ModelMainActivity.class);
        intent.putExtra("index", index);
        activity.startActivityForResult(intent, 996);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        int index = intent.getIntExtra("index", 0);
//        getHeadBar().getCenterContainer().setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
//        getHeadBar().setRightIcon(R.drawable.icon_search, new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                jump2Activity(SearchActivity.class);
//            }
//        });
        titleListener = new TitleListener() {

            @Override
            public void onChangeMain() {
                if (itemsDialog == null) {
                    itemsDialog = new ItemsDialog(activity, new String[]{"连合作", "约技能"}, new ItemsDialog.ItemChooseListener() {
                        @Override
                        public void choose(ItemsDialog dialog, int position) {
                            dialog.dismiss();
                            showFragment(position);
                        }
                    });
                }
                itemsDialog.show();
            }
        };
        showFragment(index);
    }

    private void showFragment(int index) {
        if (currentIndex == index) {
            return;
        }
//        if (index == 0) {
//            getHeadBar().setTitle("连合作", 0, R.drawable.icon_arrow_down_white);
//        } else if (index == 1) {
//            getHeadBar().setTitle("约技能", R.drawable.icon_arrow_down_white, R.drawable.icon_arrow_down_white);
//        } else if (index == 2) {
//            getHeadBar().setTitle("换着玩", 0, R.drawable.icon_arrow_down_white);
//        }
        if (index == 0) {
            if (renCaiMainFragment == null) {
                renCaiMainFragment = new RenCaiMainFragment();
                renCaiMainFragment.setTiTleListener(titleListener);
                addFragment(R.id.baseActivityRootLayout, renCaiMainFragment);
            }
            showFragment(renCaiMainFragment);
            hideFragment(zhaoFuWuMainFragment);
            hideFragment(huanZheWanMainFragment);

        } else if (index == 1) {
            if (zhaoFuWuMainFragment == null) {
                zhaoFuWuMainFragment = new ZhaoFuWuMainFragment();
                zhaoFuWuMainFragment.setTiTleListener(titleListener);
                addFragment(R.id.baseActivityRootLayout, zhaoFuWuMainFragment);
            }
            showFragment(zhaoFuWuMainFragment);
            hideFragment(renCaiMainFragment);
            hideFragment(huanZheWanMainFragment);
        } else if (index == 2) {
            if (huanZheWanMainFragment == null) {
                huanZheWanMainFragment = new HuanZheWanMainFragment();
                huanZheWanMainFragment.setTiTleListener(titleListener);
                addFragment(R.id.baseActivityRootLayout, huanZheWanMainFragment);
            }
            showFragment(huanZheWanMainFragment);
            hideFragment(zhaoFuWuMainFragment);
            hideFragment(renCaiMainFragment);
        }
        currentIndex = index;
    }

    public interface TitleListener {
        void onChangeMain();
    }
}
